package com.senney.nio;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @Author lanmengye@outlook.com
 * @Date 2020/12/29 22:01
 */
public class FileChannel03 {
    public static void main(String[] args) throws Exception {
        FileInputStream inputStream = new FileInputStream("D:\\file01.txt");
        FileChannel fileChannel01 = inputStream.getChannel();

        FileOutputStream outputStream = new FileOutputStream("D:\\file02.txt");
        FileChannel fileChannel02 = outputStream.getChannel();

        ByteBuffer byteBuffer = ByteBuffer.allocate(512);
        while (true) {
            byteBuffer.clear();
            int len = fileChannel01.read(byteBuffer);
            if (len == -1) break;
            byteBuffer.flip();
            fileChannel02.write(byteBuffer);
        }
        inputStream.close();
        outputStream.close();
    }
}
